package com.ctrip.payment.web.base;

import com.alibaba.fastjson.JSONObject;
import com.ckjava.xutils.Constants;
import com.ckjava.xutils.ExceptionUtils;
import com.ckjava.xutils.http.HttpResponse;
import com.ckjava.xutils.http.Page;
import com.ctrip.payment.domain.PageParamer;
import com.ctrip.payment.domain.base.BaseJpaEntity;
import com.ctrip.payment.service.base.BaseJpaCrudService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
public abstract class BaseJpaCrudController<S extends BaseJpaCrudService, E extends BaseJpaEntity> extends CorsController implements Constants {

    @Autowired
    public S service;

    @ApiOperation("根据 id 获取一个对象")
    @GetMapping("/{id}")
    public HttpResponse<E> get(
            @PathVariable Long id) {
        return HttpResponse.getReturn((E) service.get(id).get(), HTTPCODE.code_200, null);
    }

    @ApiOperation("获取所有的对象")
    @GetMapping(value = "/all")
    public HttpResponse<List<E>> findAll() {
        return HttpResponse.getReturn((List<E>) service.findAll().get(), HTTPCODE.code_200, null);
    }

    @ApiOperation("获取所有的对象")
    @GetMapping(value = "/search")
    public HttpResponse<List<E>> search(
            @RequestParam String keyword) {
        return HttpResponse.getReturn((List<E>) service.search(keyword).get(), HTTPCODE.code_200, null);
    }

    @ApiOperation("保存或者修改一个对象")
    @PostMapping
    public HttpResponse<Long> saveOrUpdate(
            @RequestBody E e) {
        return HttpResponse.getReturn((Long) service.saveOrUpdate(e).get(), HTTPCODE.code_200, null);
    }

    @ApiOperation("根据 id 删除一个对象")
    @DeleteMapping("/{id}")
    public HttpResponse delete(
            @PathVariable Long id) {
        service.delete(id);
        return HttpResponse.getReturn(null, HTTPCODE.code_200, null);
    }

    @ApiOperation("分页获取数据")
    @GetMapping(value = "/page")
    public HttpResponse<Page<E>> page(
            @ApiParam("{\n" +
                    "    \"sort\": {\n" +
                    "        \"predicate\": \"id\",\n" +
                    "        \"reverse\": true\n" +
                    "    },\n" +
                    "    \"search\": {\n" +
                    "        \"predicateObject\": {\n" +
                    "            \"name\": \"like,test\"\n" +
                    "        }\n" +
                    "    },\n" +
                    "    \"pagination\": {\n" +
                    "        \"start\": 1,\n" +
                    "        \"totalItemCount\": 0,\n" +
                    "        \"number\": 10,\n" +
                    "        \"numberOfPages\": 2\n" +
                    "    }" +
                    " }\n" +
                    "对于 predicateObject 中的参数，如果 activity=false, 传参为： \"activity\": \"=,false\"，" +
                    "如果 activity like '%false%', 传参为 \"activity\": \"like,false\"" +
                    "}")
            @RequestParam String params) {
        try {
            PageParamer pageParamer = JSONObject.parseObject(params, PageParamer.class);
            Page<E> page = (Page<E>) service.getPage(pageParamer).get();
            return HttpResponse.getReturn(page, HTTPCODE.code_200, null);
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponse.getReturn(null, HTTPCODE.code_500, ExceptionUtils.getExceptionMsg(e));
        }
    }
}
